package com.code.modules.base.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.code.common.utils.BeanUtil;
import com.code.common.utils.ResTreeUtil;
import com.code.modules.base.service.ResourceService;
import com.code.modules.base.mapper.ResourceMapper;
import com.code.modules.base.model.Resource;
import com.code.modules.base.vo.TreeNodeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 资源信息表(菜单,资源) 服务实现类
 * </p>
 *
 * @author MyBatisPlus
 * @since 2021-10-19
 */
@Service
public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> implements ResourceService {

    @Autowired
    private ResourceMapper resourceMapper;

    @Override
    public List<Resource> findResourceByRoleid(Integer roleid) {
        if(roleid == null) {roleid=0;}
        return resourceMapper.getResourceByRoleid(roleid);
    }

    @Override
    public String checkResByCode(String code) {
        return resourceMapper.getResNameByCode(code);
    }

    @Override
    public int findResIdByCode(String pcode) {
        return resourceMapper.getResIdByCode(pcode);
    }


    @Override
    public List<TreeNodeVo> resTreeNodedata(String condition) {
        List<TreeNodeVo> result = new ArrayList<TreeNodeVo>();
        List<Map<String,Object>> r_map = resourceMapper.resTreeMap(condition);
        List<TreeNodeVo> restree = BeanUtil.mapsToBeans(r_map,TreeNodeVo.class);
        result = ResTreeUtil.listTotree(restree);
        return result;
    }



}
